<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>md5_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_md5_test.js.html">md5_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.crypt.Md5Test&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.crypt.Md5Test&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line19"></a>goog.require(&#39;goog.crypt.Md5&#39;);
<a name="line20"></a>goog.require(&#39;goog.crypt.hashTester&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>var sixty = &#39;123456789012345678901234567890123456789012345678901234567890&#39;;
<a name="line24"></a>
<a name="line25"></a>function testBasicOperations() {
<a name="line26"></a>  var md5 = new goog.crypt.Md5();
<a name="line27"></a>  goog.crypt.hashTester.runBasicTests(md5);
<a name="line28"></a>}
<a name="line29"></a>
<a name="line30"></a>function testBlockOperations() {
<a name="line31"></a>  var md5 = new goog.crypt.Md5();
<a name="line32"></a>  goog.crypt.hashTester.runBlockTests(md5, 64);
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>function testHashing() {
<a name="line36"></a>  // Empty stream.
<a name="line37"></a>  var md5 = new goog.crypt.Md5();
<a name="line38"></a>  assertEquals(&#39;d41d8cd98f00b204e9800998ecf8427e&#39;,
<a name="line39"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line40"></a>
<a name="line41"></a>  // Simple stream.
<a name="line42"></a>  md5.reset();
<a name="line43"></a>  md5.update([97]);
<a name="line44"></a>  assertEquals(&#39;0cc175b9c0f1b6a831c399e269772661&#39;,
<a name="line45"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line46"></a>
<a name="line47"></a>  // Simple stream with two updates.
<a name="line48"></a>  md5.reset();
<a name="line49"></a>  md5.update([97]);
<a name="line50"></a>  md5.update(&#39;bc&#39;);
<a name="line51"></a>  assertEquals(&#39;900150983cd24fb0d6963f7d28e17f72&#39;,
<a name="line52"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line53"></a>
<a name="line54"></a>  // RFC 1321 standard test.
<a name="line55"></a>  md5.reset();
<a name="line56"></a>  md5.update(&#39;abcdefghijklmnopqrstuvwxyz&#39;);
<a name="line57"></a>  assertEquals(&#39;c3fcd3d76192e4007dfb496cca67e13b&#39;,
<a name="line58"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line59"></a>
<a name="line60"></a>  // RFC 1321 standard test with two updates.
<a name="line61"></a>  md5.reset();
<a name="line62"></a>  md5.update(&#39;message &#39;);
<a name="line63"></a>  md5.update(&#39;digest&#39;);
<a name="line64"></a>  assertEquals(&#39;f96b697d7cb7938d525a2f31aaf161d0&#39;,
<a name="line65"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line66"></a>
<a name="line67"></a>  // RFC 1321 standard test with three updates.
<a name="line68"></a>  md5.reset();
<a name="line69"></a>  md5.update(&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;);
<a name="line70"></a>  md5.update(&#39;abcdefghijklmnopqrstuvwxyz&#39;);
<a name="line71"></a>  md5.update(&#39;0123456789&#39;);
<a name="line72"></a>  assertEquals(&#39;d174ab98d277d9f5a5611c2c9f419d9f&#39;,
<a name="line73"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testPadding() {
<a name="line77"></a>  // Message + padding fits in two 64-byte blocks.
<a name="line78"></a>  var md5 = new goog.crypt.Md5();
<a name="line79"></a>  md5.update(sixty);
<a name="line80"></a>  md5.update(sixty.substr(0, 59));
<a name="line81"></a>  assertEquals(&#39;6261005311809757906e04c0d670492d&#39;,
<a name="line82"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line83"></a>
<a name="line84"></a>  // Message + padding does not fit in two 64-byte blocks.
<a name="line85"></a>  md5.reset();
<a name="line86"></a>  md5.update(sixty);
<a name="line87"></a>  md5.update(sixty);
<a name="line88"></a>  assertEquals(&#39;1d453b96d48d5e0cec4a20a71fecaa81&#39;,
<a name="line89"></a>               goog.crypt.byteArrayToHex(md5.digest()));
<a name="line90"></a>}
<a name="line91"></a>
<a name="line92"></a>function testTwoAccumulators() {
<a name="line93"></a>  // Two accumulators in parallel.
<a name="line94"></a>  var md5_A = new goog.crypt.Md5();
<a name="line95"></a>  var md5_B = new goog.crypt.Md5();
<a name="line96"></a>  md5_A.update(sixty);
<a name="line97"></a>  md5_B.update(sixty);
<a name="line98"></a>  md5_A.update(sixty + &#39;1&#39;);
<a name="line99"></a>  md5_B.update(sixty + &#39;2&#39;);
<a name="line100"></a>  assertEquals(&#39;0801d688cc107d4789ec8b9a4519f01f&#39;,
<a name="line101"></a>               goog.crypt.byteArrayToHex(md5_A.digest()));
<a name="line102"></a>  assertEquals(&#39;6e1a35ffc185d1e684d6ed281c0d4bd2&#39;,
<a name="line103"></a>               goog.crypt.byteArrayToHex(md5_B.digest()));
<a name="line104"></a>}
<a name="line105"></a>
<a name="line106"></a>function testCollision() {
<a name="line107"></a>  // Check a known collision.
<a name="line108"></a>  var A = [0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4,
<a name="line109"></a>           0x69, 0x3d, 0x9a, 0x06, 0x98, 0xaf, 0xf9, 0x5c,
<a name="line110"></a>           0x2f, 0xca, 0xb5, 0x87, 0x12, 0x46, 0x7e, 0xab,
<a name="line111"></a>           0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89,
<a name="line112"></a>           0x55, 0xad, 0x34, 0x06, 0x09, 0xf4, 0xb3, 0x02,
<a name="line113"></a>           0x83, 0xe4, 0x88, 0x83, 0x25, 0x71, 0x41, 0x5a,
<a name="line114"></a>           0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f,
<a name="line115"></a>           0xd9, 0x1d, 0xbd, 0xf2, 0x80, 0x37, 0x3c, 0x5b,
<a name="line116"></a>           0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b,
<a name="line117"></a>           0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6,
<a name="line118"></a>           0xdd, 0x53, 0xe2, 0xb4, 0x87, 0xda, 0x03, 0xfd,
<a name="line119"></a>           0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
<a name="line120"></a>           0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8,
<a name="line121"></a>           0xce, 0x54, 0xb6, 0x70, 0x80, 0xa8, 0x0d, 0x1e,
<a name="line122"></a>           0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93,
<a name="line123"></a>           0x96, 0xf9, 0x65, 0x2b, 0x6f, 0xf7, 0x2a, 0x70];
<a name="line124"></a>  var B = [0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4,
<a name="line125"></a>           0x69, 0x3d, 0x9a, 0x06, 0x98, 0xaf, 0xf9, 0x5c,
<a name="line126"></a>           0x2f, 0xca, 0xb5, 0x07, 0x12, 0x46, 0x7e, 0xab,
<a name="line127"></a>           0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89,
<a name="line128"></a>           0x55, 0xad, 0x34, 0x06, 0x09, 0xf4, 0xb3, 0x02,
<a name="line129"></a>           0x83, 0xe4, 0x88, 0x83, 0x25, 0xf1, 0x41, 0x5a,
<a name="line130"></a>           0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f,
<a name="line131"></a>           0xd9, 0x1d, 0xbd, 0x72, 0x80, 0x37, 0x3c, 0x5b,
<a name="line132"></a>           0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b,
<a name="line133"></a>           0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6,
<a name="line134"></a>           0xdd, 0x53, 0xe2, 0x34, 0x87, 0xda, 0x03, 0xfd,
<a name="line135"></a>           0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
<a name="line136"></a>           0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8,
<a name="line137"></a>           0xce, 0x54, 0xb6, 0x70, 0x80, 0x28, 0x0d, 0x1e,
<a name="line138"></a>           0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93,
<a name="line139"></a>           0x96, 0xf9, 0x65, 0xab, 0x6f, 0xf7, 0x2a, 0x70];
<a name="line140"></a>  var digest = &#39;79054025255fb1a26e4bc422aef54eb4&#39;;
<a name="line141"></a>  var md5_A = new goog.crypt.Md5();
<a name="line142"></a>  var md5_B = new goog.crypt.Md5();
<a name="line143"></a>  md5_A.update(A);
<a name="line144"></a>  md5_B.update(B);
<a name="line145"></a>  assertEquals(digest, goog.crypt.byteArrayToHex(md5_A.digest()));
<a name="line146"></a>  assertEquals(digest, goog.crypt.byteArrayToHex(md5_B.digest()));
<a name="line147"></a>}
</pre>


</body>
</html>
